<?php

namespace App\Http\Controllers\General;

use App\Common\Traits\LogTrait;
use App\Http\Controllers\Controller;
use App\Services\Sms\DayuSmsService;
use Illuminate\Http\Request;

class SmsController extends Controller
{
    use LogTrait;

    //
    public function sendRegisterSms(Request $request, DayuSmsService $smsService)
    {
        if(!$request->filled('phone_no')){
            return $this->fail('手机号不能为空');
        }
        $templateCode=config('strator.dayu_sms.register_template_code');
        $smsCode=mt_rand(100000,999999);
        cache(['register_code_'.$request->phone_no=>$smsCode], 300);
        $templateParam=[
            'code'=>$smsCode
        ];
        $response=$smsService->sendSms($request->phone_no,$templateCode,$templateParam);
        if($response->code!='OK'){
            $this->saveSystemLog('短信发送失败',$response,$request->phone_no);
            return $this->fail('短信发送失败,请稍后重试');
        }
        return $this->success($response,'短信已发送，请注意查收');
    }
    //
    public function sendLoginSms(Request $request, DayuSmsService $smsService)
    {
        if(!$request->filled('phone_no')){
            return $this->fail('手机号不能为空');
        }
        $templateCode=config('strator.dayu_sms.login_template_code');
        $smsCode=mt_rand(100000,999999);
        cache(['login_code_'.$request->phone_no=>$smsCode], 300);
        $templateParam=[
            'code'=>$smsCode
        ];
        $response=$smsService->sendSms($request->phone_no,$templateCode,$templateParam);
        if($response->code!='OK'){
            $this->saveSystemLog('短信发送失败',$response,$request->phone_no);
            return $this->fail('短信发送失败,请稍后重试');
        }
        return $this->success($response,'短信已发送，请注意查收');
    }
    //
    public function sendPasswordResetSms(Request $request, DayuSmsService $smsService)
    {
        if(!$request->filled('phone_no')){
            return $this->fail('手机号不能为空');
        }
        $templateCode=config('strator.dayu_sms.reset_template_code');
        $smsCode=mt_rand(100000,999999);
        cache(['reset_code_'.$request->phone_no=>$smsCode], 300);
        $templateParam=[
            'code'=>$smsCode
        ];
        $response=$smsService->sendSms($request->phone_no,$templateCode,$templateParam);
        if($response->code!='OK'){
            $this->saveSystemLog('短信发送失败',$response,$request->phone_no);
            return $this->fail('短信发送失败,请稍后重试');
        }
        return $this->success($response,'短信已发送，请注意查收');
    }


}
